const db = require('../db/index')

// 获取后台订单管理信息列表
exports.orderList = (req, res) => {
  // 每页几条数据
  let pagesize = 5
  // 当前页码数
  let pagenum = (req.query.pagenum - 1) * pagesize
  let datas = []
  const sql = `select * from pet_order limit ${pagenum},${pagesize}`
  const sql2 = `select count(*) as total from pet_order `
  db.query(sql, (err, results) => {
    if (err) return res.cc(err)
    if (results.length < 0) return res.cc('获取后台订单管理信息失败！')
    ;(datas = results),
      db.query(sql2, (err, result) => {
        if (err) return res.cc(err)
        if (result.length < 0) return res.cc('获取后台订单管理信息失败！')
        res.send({
          status: 0,
          message: '获取后台订单管理信息成功！',
          data: datas,
          total: result[0]
        })
      })
  })
}

// 搜索根据订单编号搜索订单信息
exports.searchOrder = (req, res) => {
  const queryStr = `%${req.query.oid}%`
  const sql = `select * from pet_order where oid like ? `
  db.query(sql, queryStr, (err, results) => {
    if (err) return res.cc(err)
    if (results.length < 0) return res.cc('没有您查找的订单！')
    res.send({
      status: 0,
      message: '查找的订单成功！',
      data: results
    })
  })
}

// 宠物发货
exports.isSend = (req, res) => {
  const sql = `update pet_order set isSend = 1 where oid = ?`
  db.query(sql, [req.body.oid], (err, results) => {
    if (err) return res.cc(err)
    if (results.length < 0) return res.cc('发货失败！')
    res.send({
      status: 0,
      message: '发货成功！'
    })
  })
}

// 取消订单
exports.isCancel = (req, res) => {
  const sql = `update pet_order set isCancel = 1 where oid = ?`
  db.query(sql, [req.body.oid], (err, results) => {
    if (err) return res.cc(err)
    if (results.length < 0) return res.cc('取消订单失败！')
    res.send({
      status: 0,
      message: '取消订单成功！'
    })
  })
}
